/**
 * 
 */

var taxinfo; //全局变量，保存信息
var numberoftaxinfo; //保存打印了多少张税票
var totaltaxinfo; //保存需打印的税票总数
var ticketinfo; //税票信息
var ticketamout;
var ticketnumber;
var cancelticketnum;
var pzzlDm;

//data为JSON方式
function initDeclare(tax,ticket,ticketnum)//,returnurltmp);//'system/ticket/putticket.do'tmp,printdivtmp,previewdivtmp,checkmessagetmp)
{
	//taxinfo=JSON.parse(tax);
	//ticketinfo=JSON.parse(ticket);
	//ticketamout=JSON.parse(ticketnum);
	//aa=JSON.stringify(taxinfo);
	taxinfo=tax;   //申报信息
	ticketinfo=ticket;  //税票信息
	ticketamout=ticketnum; //税票总数
	numberoftaxinfo=0; //准备打印的申请号
	ticketnumber=0;  //当前使用的税票
	cancelticketnum=0; //作废的票数
	totaltaxinfo=taxinfo.length; //需打印的数量
	//alert(taxinfo);
	//returnurl=returnurltmp;
	//'system/ticket/putticket.do'='system/ticket/putticket.do'tmp;
	//printdiv=printdivtmp;
	//previewdiv=previewdivtmp;
	//checkmessage=checkmessagetmp;


	//alert( "2:   "   +  prePath+postPath);
}
/*
//点击打印按钮，进行打印装配
function startprint()
{
	DisplayPrint();
	
}

//显示预览区
function DisplayPrint()
{
	$.blockUI({ message: $(previewdiv),
		css: { top:  ($(window).height() - 500) /2 + 'px', 
        left: ($(window).width() - 780) /2 + 'px', width: '780px',height: '500px',overflow: 'auto' } }); //显示要打印的信息

	parseData();
}
*/
//代入数据
function parseData()
{
	
	ticketinfo[ticketnumber].kpJe=taxinfo[numberoftaxinfo].kpJe;
	//代入税票号码
	$("span.ticketnumber").text(ticketinfo[ticketnumber].sphmQ);
	//代入申报信息
	for(i=0;i<taxinfo[numberoftaxinfo].bzxx.length;i++)
	{
		$("span.ccdjh"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].ccdjh);
		$("span.sbfsDm"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].sbfsDm);
		$("span.zsxmDm"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].zsxmDm);
		$("span.zspmDm"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].zspmDm);
		$("span.ssqqRq"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].ssqqRq);
		$("span.ssqzRq"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].ssqzRq);
		$("span.jkqxRq"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].jkqxRq);
		$("span.jsyj"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].jsyj);
		$("span.sl"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].sl);
		$("span.yjsJe"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].yjsJe);
		$("span.jmlxDm"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].jmlxDm);
		$("span.jmsJe"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].jmsJe);
		$("span.kcJe"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].kcJe);
		$("span.znjBj"+(i+1)).text(taxinfo[numberoftaxinfo].bzxx[i].znjBj);
		
	}
}
//获取税票号码并提示
/*
function GetTicketNum()
{
	$(data).block({ 
        message: '<h1>正在读取税票信息</h1>', 
        css: { border: '3px solid #a00' } 
    }); //在悬浮窗显示

	$.get(url,null,function(ticketjason)
			{
				var ticketinfo=JSON.parse(ticketjason);//把传入的税票信息变成JSON对象
				$(data).unblock(); //解锁悬浮窗
				if(ticketinfo.ticketstatus=="N")
				{
					$(data).block({ 
				        message: '<h1>税票已用完，无法打印</h1><p><input type="button" value="返回" onclick="location.href="'+returnurltmp+'""/>', 
				        css: { border: '3px solid #a00' } 
				    }); //如果没税票信息，显示信息，按确定就返回搜索界面
				}
				else
				{
					//输出税票信息
					InfoticketNum(ticketinfo,data);//有税票，进入税票提示
				}
			});
}*/

//提示放入指定税票
function InfoTicketNum()
{
	//锁定窗口，显示放入税票信息，按确定开始打印
		$.blockUI({
			message:  '<table><tr><th align="center" style="font-size:12px;">请放入字别：<font color="red">'+ticketinfo[ticketnumber].zb+'</font>，号码：<font color="red">'+ticketinfo[ticketnumber].sphmQ+'</font>的税票</th></tr><tr><td align="center"><input type="button" value="确定" class="system_button" onclick="startprint()"></td></tr></table>'

	}
			
	);
}

//打印
function startprint()
{
	//$.unblockUI();
	//$.unblockUI();//解锁总框架
	//锁定总框，显示是否打印成功的提示
	//成功，测试有无打印信息，有就提示，没有就返回搜索界面 
	//重打，，重新打印
	//打废，重新进入DisplayPrint()
	//$("div.printArea").printArea();

	bdhtml=window.document.body.innerHTML;//获取当前页的html代码
	sprnstr="<!--startprint1-->";//设置打印开始区域
	eprnstr="<!--endprint1-->";//设置打印结束区域
	prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18); //从开始代码向后取html

	prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
	window.document.body.innerHTML=prnhtml; //代入需打印的内容
	window.print(); //打印
	window.document.body.innerHTML=bdhtml; //把原内容代回
	//$.unblockUI();
	$.blockUI({ 
		message:  '<table><tr><th colspan="2" align="center">请检查是否打印成功?</th></tr><tr><td align="left"><input type="button" value="打印成功" class="system_button" onclick="saveticket()"></td><td align="right"><input type="button" value="打印失败" class="system_button" onclick="printFail()"></td></tr></table>'
	}); 
}
//打印失败
function printFail(){
	//$.unblockUI();
	$.blockUI({ 
		message:  '<table><tr><td align="left" style="font-size:12px;">税票完整，但没打印成功，选择<input type="button" value="重新打印" class="system_button" onclick="reprint()"></td></tr><tr><td align="left" style="font-size:12px;">税票打坏，打印不成功。选择<input type="button" value="作废税票并重新打印" class="system_button" onclick="reprintwithticket()"></td></tr></table>'
	}); 
}
//重新打印
function reprint()
{
	//$.unblockUI();//解锁总框架
	//锁定总框，显示是否打印成功的提示
	//成功，测试有无打印信息，有就提示，没有就返回搜索界面 
	//重打，，重新打印
	//打废，重新进入DisplayPrint()
	//DisplayPrint();
	//$.unblockUI();
	InfoTicketNum();
	/*
	$.blockUI({
		message: '<h1>请放入字别为：'+ticketinfo[ticketnumber].zb+'，标号为：'+ticketinfo[ticketnumber].sphmQ+'的税票</h><p><input type="button" value="确定" onclick="print()"/></p>', 
        css: { border: '3px solid #a00' } 	 });*/
}
function reprintwithticket()
{
	//pzzlDm=ticketdm;
	//$.unblockUI();//解锁总框架
	$.post('system/ticket/putticket.do',{ywlsSt:JSON.stringify(ticketinfo[ticketnumber]),declareSt:"02",bj:"cancel"},function(data)
			{
					if(data=='Y')
					{
						//$.unblockUI();
						cancelticketnum++;
						ticketnumber++;
						tip();
						
						
					}
				else
				{
					//$.unblockUI();
					alert("数据库错误！");
					window.location.href=returnurl;
				}
				
			});
	
	
}
//获取新的税票号，用于税票打废的情况
function regetticket()
{
	$.get('system/ticket/getticket.do?pzzlDm=02&length='+cancelticketnum,null,function(data){
		
		try
		{
			if(data=="N")
			{
				cancelticketnum=0;
				tip();
			}
			else
			{
				var tmpjson=JSON.parse(data);
				for(i=0;i<tmpjson.length;i++)
					ticketinfo.push(tmpjson[i]);
				ticketamout.realticketnum+=tmpjson.length;
				cancelticketnum=0;
				//$.unblockUI();
				//alert("获得"+tmpjson.length+"张税票！");
				tip();
			}
		}
		catch(e){
			//$.unblockUI();
			alert("数据库错误！");
			window.location.href=returnurl;
		}
	});
	
}
//提示有无需要打印的内容，有继续打，没有返回搜索页面。
function tip()
{
	if((numberoftaxinfo<totaltaxinfo)&&(ticketnumber<ticketamout.realticketnum))//需要打印
	{
		parseData();
		InfoTicketNum();
		//DisplayPrint();
	}
	else
	{
		if (cancelticketnum==0)
		{
			alert("共需打印 "+totaltaxinfo+" 张税票,有效打印数量:"+ticketamout.ticketnum+",作废数量:"+Math.abs(ticketamout.realticketnum-ticketamout.ticketnum));
			window.returnValue=1;
			window.close();
			//window.location.href=returnurl;
		}
		else
		{
			regetticket();
		}
	}
}
//保存当前税票以及申报信息(自助)
function saveticket()
{
	//$.unblock();
	$.post('system/ticket/putticket.do',{ywlsSt:JSON.stringify(ticketinfo[ticketnumber]),declareSt:JSON.stringify(taxinfo[numberoftaxinfo].pzxx[0]),bj:""},function(data){
		if(data=='Y')
		{
			//$.unblockUI();
			//alert("保存成功！");
			//清空一次
			for(i=0;i<taxinfo[numberoftaxinfo].bzxx.length;i++)
			{
			$("span.ccdjh"+(i+1)).text('');
			$("span.sbfsDm"+(i+1)).text('');
			$("span.zsxmDm"+(i+1)).text('');
			$("span.zspmDm"+(i+1)).text('');
			$("span.ssqqRq"+(i+1)).text('');
			$("span.ssqzRq"+(i+1)).text('');
			$("span.jkqxRq"+(i+1)).text('');
			$("span.jsyj"+(i+1)).text('');
			$("span.sl"+(i+1)).text('');
			$("span.yjsJe"+(i+1)).text('');
			$("span.jmlxDm"+(i+1)).text('');
			$("span.jmsJe"+(i+1)).text('');
			$("span.kcJe"+(i+1)).text('');
			$("span.znjBj"+(i+1)).text('');
			}
			//
			numberoftaxinfo++;
			ticketnumber++;
			
			tip();
			
		}
		else
		{
			//$.unblockUI();
			alert("数据库错误！");
			window.location.href=returnurl;
		}
		
	});
	
	
	//$.blockUI();
}

function returnunblock()
{
	$.unblockUI();
}
